local CorePropUIHelp = {}

local Configs = {
    [Enums.CoreProp.Strength] = {icon = Res.CorePropIcon.Strength, text = L("CoreProp_Strength")},
    [Enums.CoreProp.Intelligence] = {icon = Res.CorePropIcon.Intelligence, text = L("CoreProp_Intelligence")},
    [Enums.CoreProp.Agility] = {icon = Res.CorePropIcon.Agility, text = L("CoreProp_Agility")},
    [Enums.CoreProp.Endurance] = {icon = Res.CorePropIcon.Endurance, text = L("CoreProp_Endurance")},
    [Enums.CoreProp.Spirit] = {icon = Res.CorePropIcon.Spirit, text = L("CoreProp_Spirit")}
}

function CorePropUIHelp.getPorpDescArr(tp, level)
    local tStr = {}
    local t = GD:queryCorePorpValue(UD:getClass(), tp, level)
    if #t == 0 then
        print(string.format("can not find core prop config class = %d  type = %s  level = %s", UD:getClass(), tp, level))
        -- assert(false, string.format("can not find core prop config class = %d  type = %s  level = %s", UD:getClass(), tp, level))
        return false, {}
    end
    for k ,v  in ipairs(t) do
        if v.tp ~= nil  and v.tp ~= 0 then
            local name, value = GD:getPropShowData(v.tp, v.value) 
            if value == nil then
                value = ""
            else
                value = "+"..value
            end
            table.insert(tStr, string.format( "%s %s",name or "", value))
        end
    end
    return true, tStr
end

function CorePropUIHelp.getTipsContent(tp)
    local tStr = {}
    local t = GD:queryCorePorpValue(UD:getClass(), tp, 1)
    if #t == 0 then
        print(string.format("can not find core prop config class = %d  type = %s  level = %s", UD:getClass(), tp, level))
        -- assert(false, string.format("can not find core prop config class = %d  type = %s  level = %s", UD:getClass(), tp, level))
        return ""
    end

    local name
    for i = #t, 1, -1 do
        local temp = t[i]
        if temp.tp ~= nil and temp.tp ~= 0 then
            name = GD:getPropShowData(temp.tp, temp.value) 
            break
        end
    end
    name = string.gsub(name or "", "：+","") or ""
    name = string.gsub(name or "", ":+","") or ""
    local props = t[#t]
    return string.format(L("CoreProp_Tips"), CorePropUIHelp.getTextByType(tp), name, CorePropUIHelp.getTextByType(tp))
end

-- 获取下一下 阶段的属性点值
function CorePropUIHelp.getNextStageLevel(level)
    local nextStageLevel = math.ceil((level + 1)/10) * 10
    local maxLevel = Constant.CoreProp_MaxLevel
    if nextStageLevel > maxLevel then
        nextStageLevel = maxLevel
    end
    return nextStageLevel
end


function CorePropUIHelp.getMaxLevelDiff()
    return tonumber(GD:getSystemPar("CorePropStep", 10))
end

function CorePropUIHelp.getMaxAddLevel(tp)
    local rows = GD:queryCorePorpExtraValues(UD:getClass())
    local maxMountStar = tonumber(rows[#rows]["addType"..tp]) or 0
    return Constant.CoreProp_MaxLevel - maxMountStar
end

function CorePropUIHelp.getMaxExchangePoint()
    return tonumber(GD:getMaxSumPoint())
end

function CorePropUIHelp.checkCanAddLevel(tp)
    local t =  {}
    local canAdd = true
    local maxDiff = CorePropUIHelp.getMaxLevelDiff()

    local values = UD.coreProp:getValues()
    local curVaule = UD.coreProp:getValueByType(tp)
  
    if curVaule >= CorePropUIHelp.getMaxAddLevel(tp) then
        return false, {}, L("CorePropMaxLevel"), true
    end
    
    for k, v in pairs(values) do
        if k ~= tp and curVaule - v >= maxDiff then
            table.insert(t, k)
        end
    end

    local canNotAddLevelDesc = "" 
    local canNotAddLevelMap = {}
    if #t > 0 then
        canAdd = false
        local descTexts = {}
        for k, v in ipairs(t) do
            canNotAddLevelMap[v] = true
            table.insert(descTexts, CorePropUIHelp.getTextByType(v))
        end
        local desc = ""
        if app:isLanguageEN() then
            desc = table.concat(descTexts, ", ")
        else
            desc = table.concat(descTexts, "、")
        end
        
        if #descTexts == 1 then
            canNotAddLevelDesc = string.format( L("CorePropCanNotAddLevelDesc3"), desc)
        else
            canNotAddLevelDesc = string.format( L("CorePropCanNotAddLevelDesc2"), desc)
        end
    end
    return canAdd, canNotAddLevelMap, canNotAddLevelDesc, false
end


function CorePropUIHelp.getIconByType(t)
   local conf = Configs[t]
   assert(conf ~= nil, "can not find core porp config by type "..(t or nil))
   return conf.icon
end


function CorePropUIHelp.getTextByType(t)
    local conf = Configs[t]
   assert(conf ~= nil, "can not find core porp config by type")
   return conf.text
end

function CorePropUIHelp.getCorePropItemNum()
    return UD:getItemAmountByTid(Constant.CoreProp_ItemId)
end

function CorePropUIHelp.getCorePropItemCost(sumPoint)
    return GD:queryCorePorpAddPointCostBySumPoint(sumPoint)
end

function CorePropUIHelp.getPropOrderByClass()
    local key = string.format("CorePropClass%s", UD:getClass())
    
    local order = GD:getSystemStrPar(key, "1|2|3|4|5")
    local orderStrArr = string.split(order, "|")
    local orderArr = {}
    for k, v in pairs(orderStrArr) do
        table.insert(orderArr, tonumber(v))
    end
    return orderArr
end


function CorePropUIHelp.getRadarData(values, orderArr)
    local maxVaule = 0
    local maxLevel = Constant.CoreProp_MaxLevel
    local maxDiffLevel = CorePropUIHelp.getMaxLevelDiff()
    for k, v in pairs(values) do
        if v > maxVaule then
            maxVaule = v
        end
    end
    
    local scale = {}
    for k ,v in pairs(orderArr) do
        table.insert(scale,  (maxVaule - values[v])/maxDiffLevel)
    end

    local d = {
        baseScale = maxVaule/maxLevel,
        scale = scale
    }

    return d
end


function CorePropUIHelp.getCurMountStarConfig()
    local moundModel = app:getInst("MountModel")
    local mountStarNum = 0
    if moundModel then
        mountStarNum = moundModel:getMountsTotalStar()
    end
    
    local curStarConfig = nil
    local rows = GD:queryCorePorpExtraValues(UD:getClass())
    for k , v in ipairs(rows) do
        local t = tonumber(v.starNum or 0)
        if mountStarNum < t then
            curStarConfig = rows[k-1]
            break
        end
    end

    if not curStarConfig then
        curStarConfig = rows[#rows]
    end

    return curStarConfig
end
return CorePropUIHelp